const service = require('../request').service;
const model = require("../../utils/modelUtil");

/**
 * 玩家类
 */
class Player {
    constructor(user) {
        this.user = user;
    }

    /**
     * 获取游戏信息
     * @returns {Promise<unknown>}
     */
    getLoadGameData() {
        const data = {
            TEObj: {
                os: 'Windows',
                zone_offset: 8,
                distinct_id: '0832260469-1713716419109',
                device_id: '5881032514-1713716419109',
                server_id: this.user.game.serverName
            }
        }
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/LoadGame',
                data: model.userModel('LoadGame', data, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }

    /**
     * 获取福缘
     * @returns {Promise<unknown>}
     */
    getLucky() {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/GetChatAddress',
                data: model.userModel('GetChatAddress', {}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }

    /**
     * 使用物品
     * @param id 物品ID
     * @param number 使用数量
     * @param options 其它扩展参数
     * @returns {Promise<unknown>}
     */
    useItem(id, number, options) {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/useItem',
                data: model.userModel('useItem', {itemID: id, itemNum: number, ...options}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }

    /**
     * 结束打桩
     * @returns {Promise<unknown>}
     */
    endHitPile() {
        const data = {
            artLevel: this.user.game.player[33],
            maxHP: this.user.game.player[28],
            maxMP: this.user.game.player[29],
            hglmt: 760,
            speed: 73
        };
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/EndPlayWooks',
                data: model.userModel('EndPlayWooks', data, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }
}

module.exports = Player;